﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BussinesLayer.Patients;
using ModelLayer;

namespace ViewLayer.Patients
{
    public partial class SearchPatientView : Form
    {
        private int loadType;
        MedAgendaEntities entities = new MedAgendaEntities();
        public SearchPatientView(int loadType)
        {
            InitializeComponent();
            this.loadType = loadType;
        }

        private void patientProfile_DataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            this.Cursor = Cursors.AppStarting;
            if (e.RowIndex >= 0)
            {
                AgendaPatientController.currentPID = patientProfile_DataGridView.Rows[e.RowIndex].Cells["PID"].Value.ToString();

                if (this.loadType == 1)
                {
                    string oid = patientProfile_DataGridView.Rows[e.RowIndex].Cells["OID_"].Value.ToString();

                    PatientInformationView patientInfoView = new PatientInformationView(oid, false);
                    patientInfoView.ShowDialog();
                    this.Cursor = Cursors.Default;
                    this.textBox_KeyPress(null, new KeyPressEventArgs((char)13));
                }
                else
                {
                    this.Cursor = Cursors.Default;
                    this.Close();
                }
            }
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            this.textBox_KeyPress(null, new KeyPressEventArgs((char)13));
        }

        private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (!(textBoxName.Text == "" && textBoxLastName.Text == "" && textBoxCi.Text == "" && maskedTextBoxDOB.Text == "  /  /"))
                {
                    if (textBoxName.Text == "")
                        textBoxName.Text = null;
                    if (textBoxLastName.Text == "")
                        textBoxLastName.Text = null;
                    if (textBoxCi.Text == "")
                        textBoxCi.Text = null;
                    try
                    {
                        if (!(maskedTextBoxDOB.Text == "  /  /"))
                        {
                            DateTime fecNac1 = DateTime.Parse(maskedTextBoxDOB.Text);
                            DateTime fecNac2 = DateTime.Parse(maskedTextBoxDOB.Text).AddDays(1);
                            var patients = from p in entities.PatientProfile_
                                           where p.FamilyName_.StartsWith(textBoxLastName.Text)
                                           && p.GivenName_.StartsWith(textBoxName.Text)
                                           && p.IdDocumentNumber_.StartsWith(textBoxCi.Text)
                                           && (((DateTime)p.DateOfBirth_) < fecNac2 && ((DateTime)p.DateOfBirth_) > fecNac1)
                                           select new
                                           {
                                               OID_ = p.OID_,
                                               Apellido = p.FamilyName_,
                                               Nombre = p.GivenName_,
                                               SegNombre = p.MiddleName_,
                                               FecNac = p.DateOfBirth_,
                                               Sexo = p.SexEnum__Code_,
                                               PID = p.MrnId_,
                                               CI = p.IdDocumentNumber_
                                           };
                            patientProfile_DataGridView.DataSource = patients;
                        }
                        else
                        {
                            var patients = from p in entities.PatientProfile_
                                           where p.FamilyName_.StartsWith(textBoxLastName.Text)
                                           && p.GivenName_.StartsWith(textBoxName.Text)
                                           && p.IdDocumentNumber_.StartsWith(textBoxCi.Text)
                                           select new
                                           {
                                               Apellido = p.FamilyName_,
                                               Nombre = p.GivenName_,
                                               SegNombre = p.MiddleName_,
                                               FecNac = p.DateOfBirth_,
                                               Sexo = p.SexEnum__Code_,
                                               CI = p.IdDocumentNumber_,
                                               OID_ = p.OID_,
                                               PID = p.MrnId_
                                           };
                            patientProfile_DataGridView.DataSource = patients;
                        }
                    }
                    catch (System.Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message);
                    }
                }
            }
        }
    }
}
